﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGen.TS.Generation.Members
{

    public class TSPropertyInfo : IMemberInfo
    {
        public TSPropertyInfo(Type tp)
        {
            this.OriginalCLRType = tp;
        }

        public TSPropertyInfo(System.Reflection.PropertyInfo propertyInfo)
            : this(propertyInfo.PropertyType)
        {

            this.MemberName = propertyInfo.Name;
        }
        public bool IsArray { get; set; }

        public Type OriginalCLRType { get; set; }
        public IntegratedTypescriptType ScriptType { get; set; }
        public string MemberName { get; set; }
        public string InitialValue { get; set; }
        public string ComplexTypeName { get; set; }
        public bool UseObservable
        {
            get;
            set;
        }

        public string GetDefinition(bool withInitialValue)
        {
            switch (this.ScriptType)
            {
                case IntegratedTypescriptType.Bool:
                    if (InitialValue == null)
                    {
                        InitialValue = "false";

                    }

                    break;
                case IntegratedTypescriptType.Number:
                    if (InitialValue == null)
                    {
                        InitialValue = "0";
                    }

                    break;
                case IntegratedTypescriptType.String:
                    if (InitialValue == null)
                    {
                        InitialValue = "null";
                    }

                    break;
                case IntegratedTypescriptType.Date:
                    if (InitialValue == null)
                    {
                        InitialValue = "new Date()";
                    }

                    break;
                case IntegratedTypescriptType.Complex:
                    {
                        if (InitialValue == null)
                        {
                            InitialValue = "null";
                        }
                    }
                    break;
                case IntegratedTypescriptType.Unknown:
                    if (InitialValue == null)
                    {
                        InitialValue = "null";
                    }
                    break;
            }
            string typeName = this.GetTypeName();
            if (UseObservable)
            {
                if (withInitialValue)
                {
                    if (ScriptType == IntegratedTypescriptType.Complex)//For whatever reason setting null does throw a typescript compile error
                        return MemberName + " : " + typeName + " = ko.observable(<any>" + InitialValue + ")" + ";";
                    else
                        return MemberName + " : " + typeName + " = ko.observable(" + InitialValue + ")" + ";";
                }
                else
                    return MemberName + " : " + typeName + ";";
            }
            else
            {
                return MemberName + " : " + typeName + (withInitialValue ? "= " + InitialValue : "") + ";";
            }

        }


    }

}
